package com.fowo.api.controller.shipment.fbainvoice;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.FbaShipment;
import com.fowo.api.model.fbainvoice.FBAInvoiceShippingAction;
import com.fowo.api.model.fbainvoice.FBAInvoiceVo;
import com.fowo.api.model.stock.out.StockOutVo;
import com.fowo.api.service.StockOutService;
import com.fowo.api.service.handleWork.FBAInvoicePushStockOut;
import com.fowo.api.service.impl.FBAInvoiceServiceImpl;
import com.fowo.api.service.impl.FbaShipmentServiceImpl;
import com.fowo.api.sys.entity.SysUser;
import com.fowo.api.sys.service.SysUserService;
import com.fowo.api.sys.service.impl.SysUserServiceImpl;
import com.fowo.api.user.model.JwtUserInfo;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Component()
public class fbaInvoiceShippingAction implements FBAInvoiceShippingAction {
    @Resource
    private FBAInvoiceServiceImpl fbaInvoiceServiceImpl;
    @Resource
    private FBAInvoicePushStockOut fBAInvoicePushStockOut;
    //fbaShipment
    @Resource
    private FbaShipmentServiceImpl fbaShipmentService;
    @Resource
    private StockOutService stockOutService;
    @Resource
    private SysUserServiceImpl sysUserService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void run(List<Long> idList) throws Exception {
        for(int i=0;i<idList.size();i++){
            Long l = idList.get(i);
            suoku(l);
        }
    }

    private void suoku(Long l) throws Exception {

        FBAInvoiceVo voById = fbaInvoiceServiceImpl.getVoById(l);
        Assert.isTrue(voById.getIslockinventory().equals("1"),"未锁库存");
        Assert.isTrue(voById.getInvoiceStatus().equals("30"),"违规操作");
        fbaInvoiceServiceImpl.lockStockOut(l);
        voById.setInvoiceStatus("40");
        voById.setIsOut(true);
        ///sendTime
        voById.setSendTime(new Date());
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        Long userId = jwtUserInfo.getUserId();
        SysUser byId = sysUserService.getById(userId);
        voById.setSendName(byId.getName());
        fbaInvoiceServiceImpl.update(voById);
        //生成出库单
        StockOutVo stockOutVo = fBAInvoicePushStockOut.buildStockOut(voById);
        stockOutService.create(stockOutVo);
        //亚马逊标记发货
        FbaShipment fbaShipment = fbaShipmentService.getOne(Wrappers.<FbaShipment>query().lambda()
                .eq(FbaShipment::getShipmentId, voById.getShipmentCode()));
        if(fbaShipment!=null){

        }
    }
}
